package com.itheima.reggie.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.itheima.reggie.entity.Category;
import com.itheima.reggie.mapper.CategoryMapper;
import com.itheima.reggie.mapper.DishMapper;
import com.itheima.reggie.mapper.SetmealMapper;
import com.itheima.reggie.service.CategoryService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
@Slf4j
public class CategoryServiceImpl implements CategoryService {
    @Autowired
    DishMapper dishMapper;
    @Autowired
    SetmealMapper setmealMapper;
    @Autowired
    private CategoryMapper categoryMapper;

    @Override
    public void insertNewCategory(Category category) {
        categoryMapper.insertNewCategory(category);
    }

    @Override
    public PageInfo<Category> selectByPage(int page, int pageSize) {
        //开启分页,通过某种方式改变了SQL语句,后面加了limit  起始索引, 查询记录数
        PageHelper.startPage(/*当前第几页*/page,/*每页多少条*/pageSize);
        //查询所有,只返回指定页
        List<Category> list = categoryMapper.selectAllOrderBySort();
        //封装PageInfo
        PageInfo<Category> pageInfo = new PageInfo<>(list);
        return pageInfo;
    }

    @Override
    public String deleteById(String categoryId) {
        Integer dishNumberUseCategoryId = dishMapper.countByCategoryId(categoryId);
        if (dishNumberUseCategoryId != 0) {
            return "当前分类下关联了菜品，不能删除";
        }
        Integer setmealNumberUsecCategoryId = setmealMapper.countByCategoryId(categoryId);
        if (setmealNumberUsecCategoryId != 0) {
            return "当前分类下关联了套餐，不能删除";
        }
        categoryMapper.deleteById(categoryId);
        return "success";//表示删除类别成功
    }

    @Override
    public void updateById(Category category) {
        categoryMapper.updateById(category);
    }

    @Override
    public List<Category> selectByType(Integer type) {
        return categoryMapper.selectByType(type);
    }

    @Override
    public List<Category> selectAll() {
        return categoryMapper.selectAll();
    }
}
